//
//  UIImage+My.m
//  My00
//
//  Created by 黄杰 on 15/1/21.
//  Copyright (c) 2015年 黄杰. All rights reserved.
//

#import "UIImage+My.h"
#import "NSString+HJ.h"
#import <Accelerate/Accelerate.h>

@implementation UIImage (My)

+ (UIImage *)resizeWithImageName:(NSString *)imageName
{
    return [self resizeImageWithName:imageName left:0.5 top:0.5];;
}

+ (UIImage *)resizeImageWithName:(NSString *)name left:(CGFloat)left top:(CGFloat)top
{
    UIImage *image = [self imageNamed:name];
    return [image stretchableImageWithLeftCapWidth:image.size.width * left topCapHeight:image.size.width * top];
}

+ (UIImage *)fullScreenImageName:(NSString *)name
{
    if (isIphone6_Up || isIphone5) {
        name = [name fileNameAppendString:@"-568h@2x"];
    }
    return [self imageNamed:name];
}

/**
 *  返回一张可以随意拉伸不变形的图片
 *
 *  @param messageFrame 图片名字
 */
+ (UIImage *)resizeableImage:(NSString *)name
{
    UIImage *normal = [UIImage imageNamed:name];
    CGFloat w = normal.size.width * 0.5;
    CGFloat h = normal.size.height * 0.5;
    return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];
}

/**
 *  高斯模糊图片
 *
 *  @param image 图片
 *  @param level 模糊度
 *
 *  @return 模糊后的图片
 */
+ (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)level
{
    CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey, inputImage, @"inputRadius", @(level), nil];
    CIImage *outputImage = filter.outputImage;
//    [outputImage imageByCroppingToRect:CGRectMake(0, 0, 100, 100)];
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef outImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
    return [UIImage imageWithCGImage:outImage];
}


/**
 *  制作毛玻璃图片
 *
 *  @param originImage 原始图片
 *  @param rect        需要截取的大小
 *  @param level       模糊度
 *
 *  @return 毛玻璃图片
 */
+ (UIImage *)imageWithOriginalImage:(UIImage *)originImage withCutRect:(CGRect)rect blurLevel:(CGFloat)level
{
    // 先对原始图片进行模糊化
    UIImage *blurImage = [self blurryImage:originImage withBlurLevel:level];
    
//    CIImage *inputImage = [CIImage imageWithCGImage:blurImage.CGImage];
//    CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, inputImage, kCIInputColorKey, [CIColor colorWithString:@"255 255 255 0.5"], kCIInputIntensityKey, @(0.5), nil];
//    CIImage *outputImage1 = filter.outputImage;
//    CIContext *context1 = [CIContext contextWithOptions:nil];
//    CGImageRef outImage = [context1 createCGImage:outputImage1 fromRect:[outputImage1 extent]];
//    UIImage *white = [UIImage imageWithCGImage:outImage];
    
    // 获取图片中的rect
    CGFloat imageW = originImage.size.width;
    CGFloat imageH = originImage.size.height;
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    CGFloat scaleW = imageW / screenW;
    CGFloat scaleH = imageH / screenH;
    
    CGFloat realX = rect.origin.x * scaleW;
    CGFloat realY = rect.origin.y * scaleH + 6;
    CGFloat realW = rect.size.width * scaleW;
    CGFloat realH = rect.size.height * scaleH;
    CGRect realRect = CGRectMake(realX, realY, realW, realH);
    
    // 截图
    CGImageRef imageRef = CGImageCreateWithImageInRect(blurImage.CGImage, realRect);
    UIGraphicsBeginImageContext(CGSizeMake(realW, realH));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, realRect, imageRef);
    UIImage *result = [UIImage imageWithCGImage:imageRef];
    UIGraphicsEndImageContext();
    
    return result;
    
}

/**
 *  保持原来的长宽比，生成一个缩略图
 */
+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize
{
    UIImage *newimage;
    if (nil == image) {
        newimage = nil;
    }
    else{
        CGSize oldsize = image.size;
        CGRect rect;
        if (asize.width/asize.height > oldsize.width/oldsize.height) {
            rect.size.width = asize.height*oldsize.width/oldsize.height;
            rect.size.height = asize.height;
            rect.origin.x = (asize.width - rect.size.width)/2;
            rect.origin.y = 0;
        }
        else{
            rect.size.width = asize.width;
            rect.size.height = asize.width*oldsize.height/oldsize.width;
            rect.origin.x = 0;
            rect.origin.y = (asize.height - rect.size.height)/2;
        }
        UIGraphicsBeginImageContext(asize);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
        [image drawInRect:rect];
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return newimage;
}

- (UIImage*)scaleToSize:(CGSize)size
{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    //Determine whether the screen is retina
    if([[UIScreen mainScreen] scale] == 2.0){
        UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
    }else{
        UIGraphicsBeginImageContext(size);
    }
    // 绘制改变大小的图片
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

/**
 *  根据view生成一张截图
 */
+ (UIImage *)captureWithView:(UIView *)view
{
    // 1.开启上下文
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    
    // 2.将控制器view的layer渲染到上下文
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    // 3.取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 4.结束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end
